package org.boot.easyapi.core.util.sql;


import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.Map;

import static org.boot.easyapi.exception.BusinessException.exception;


/**
 * sql操作符工具类
 */
public class SqlOperatorUtil {

    public final static String GE = " >= ";     //入参规则：入参字段名@GE=指定值
    public final static String GT = " > ";      //入参规则：入参字段名@GT=指定值
    public final static String LE = " <= ";     //入参规则：入参字段名@LE=指定值
    public final static String LT = " < ";      //入参规则：入参字段名@LT=指定值
    public final static String EQ = " = ";      //入参规则：入参字段名@EQ=指定值
    public final static String NE = " != ";     //入参规则：入参字段名@NE=指定值
    public final static String LIKE = " LIKE "; //入参规则：入参字段名@LIKE=指定值
    public final static String IN = " IN ";         //&入参字段名@IN=指定值01,指定值02
    public final static String NOTIN = " NOT IN ";  //&入参字段名@NOTIN=指定值01,指定值01
    public final static String DESC = " DESC ";         //入参规则：入参字段名@DESC=任意值
    public final static String ASC = " ASC ";           //入参规则：入参字段名@ASC=任意值
    public final static String ISNULL = " IS NULL ";        //入参规则：入参字段名@ISNULL=任意值
    public final static String NOTNULL = " IS NOT NULL ";    //入参规则：入参字段名@NOTNULL=任意值

    public final static Map<String, String> OPSMAP = new LinkedHashMap<String, String>() {{
        put("GE", " >= ");
        put("GT", " > ");
        put("LE", " <= ");
        put("LT", " < ");
        put("EQ", " = ");
        put("NE", " != ");
        put("LIKE", " LIKE ");
        put("IN", " IN ");
        put("NOTIN", " NOT IN ");
        put("DESC", " DESC ");
        put("ASC", " ASC ");
        put("ISNULL", " IS NULL ");
        put("NOTNULL", " IS NOT NULL ");
    }};



    public static Boolean isParamKey(String param){
        if(param.contains("@")){
            return true;
        }
        return false;
    }

    /**
     * 根据前端传递的判定符关键字，获取系统支持的判定符号
     * @param operator
     * @return
     */
    public static String getOperator(String operator){
        try {
            Class<?> clazz = SqlOperatorUtil.class;
            Field field = clazz.getDeclaredField(operator);
            // 获取字段的值
            return (String) field.get(null); // 静态字段传 null 作为对象
        }catch (Exception e){
//            throw exception(new ErrorCode("请按照系统所支持sql判定符进行入参！"));
            throw exception("请按照系统所支持sql判定符进行入参！");
        }
    }



    /**
     * 解析字段名部分（@ 前的部分）
     * @param param 输入字符串，格式为 "字段名@操作符关键字"
     * @return 字段名部分
     */
    public static String getField(String param) {
        try{
            if (param.contains("@")) {
                return param.split("@")[0];
            }
        } catch (Exception e) {
//            throw exception(new ErrorCode("请检查 '@' 字符串拼接是否正确，格式：入参字段名@判定标识 --> age@GE'！"));
            throw exception("请检查 '@' 字符串拼接是否正确，格式：入参字段名@判定标识 --> age@GE'！");
        }
        return param;

    }

    /**
     * 解析操作符关键字部分（@ 后的部分）
     * @param param 输入字符串，格式为 "字段名@操作符关键字"
     * @return 操作符关键字部分
     */
    public static String getOp(String param) {
        String op = "";
        try{
            if (param.contains("@")) {
                op = param.split("@")[1];
                String operator = getOperator(op);
                return operator;
            }
        } catch (Exception e) {
//            throw exception(new ErrorCode("请检查'@'拼接格式是否正确 以及 系统是否支持该判定符："+op+"！"));
            throw exception("请检查'@'拼接格式是否正确 以及 系统是否支持该判定符：" + op + "！");
        }
        return EQ;
    }



//    public static void main(String[] args) {
//        String input = "name@GE";
//        System.out.println(getField(input));
//        System.out.println(getOp(input));
//    }
//













}
